﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Groupbox;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Windows.Browser;

namespace OntoWPFSilverlight.Widget
{
    [OntoWPFSilverlight.Annotation.Widget("CheckButtonGroupBox")]
    public class CheckButtonGroupBox : WidgetCanvas, Interfaces.IWidgetWithItems
    {
        
        GroupBox groupBox = null;
        WrapPanel wrapPanel = null;

        public CheckButtonGroupBox()
        {
            Build();
        }

        private void Build()
        {
            groupBox = new GroupBox();
            wrapPanel = new WrapPanel();
            
            wrapPanel.Orientation = Orientation.Horizontal;
            wrapPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
            wrapPanel.VerticalAlignment = VerticalAlignment.Stretch;
            
            groupBox.Content = wrapPanel;
            groupBox.Header = "Label";
           
            DockPanel.SetDock(groupBox, Dock.Top);
            
            this.Children.Add(groupBox);
        }

        //public void AddItem(IEnumerable<XElement> nodes) 
        //{
        //    foreach (XElement node in nodes)
        //    {
        //        CheckButtonGroupBox checkBoxTemp = new CheckButtonGroupBox();
        //        checkBoxTemp.Content =  node.Attribute("Name").Value;
                
        //        //todo:
        //        //checkBoxTemp.Tag= 
        //        wrapPanel.Children.Add(checkBoxTemp);
        //    }
        //}

        public void AddLabel(string text) 
        {
            groupBox.Header = text;
        }

        public override string Text
        {
            get
            {
                return groupBox.Header.ToString();
            }
            set
            {
                groupBox.Header = value;
            }
        }

        #region IWidgetWithItems Membri di

        OntoWPFSilverlight.Node ontologyNode;

        public OntoWPFSilverlight.Node Node
        {
            get
            {
                return ontologyNode;
            }
            set
            {
                ontologyNode = value;
            }
        }

        OntoWPFSilverlight.Xml.OntologiesNodes ontologiesNodes = null;
        public OntoWPFSilverlight.Xml.OntologiesNodes OntologiesNodes
        {
            get
            {
                return ontologiesNodes;
            }
            set
            {
                this.ontologiesNodes = value;
            }
        }


        public void update(List<Node> nodes)
        {
            foreach (Node _node in nodes)
            {

                HtmlPage.Window.Alert("checkbox figli "+ _node.Name);
                CheckBox tempCheck = new CheckBox();
                tempCheck.Name = _node.Name;
                tempCheck.Content = _node.Alias;
                try
                {
                    tempCheck.FontSize = Double.Parse(this.WidgetFont.Size);
                }
                catch (Exception)
                {

                }
                //try
                //{
                //    tempCheck.FontFamily = new FontFamily(this.WidgetFont.Name);
                //}
                //catch { }
                tempCheck.IsChecked = false;
                wrapPanel.Children.Add(tempCheck);
            }
        }

        public void update()
        {
            update(this.ontologiesNodes.Nodes);
        }

        #endregion
    }
}
